// 时间美化
import moment from 'moment'
// 今天 08:00 / 昨天 15:30 / 04-23 15:00 2017-12-13 18:00
export function timeAgo (time) {
  const today = new Date()
  const todayObj = {
    y: today.getFullYear(),
    m: ('0' + (today.getMonth() + 1)).slice(-2),
    d: ('0' + today.getDate()).slice(-2),
    h: ('0' + today.getHours()).slice(-2),
    i: ('0' + today.getMinutes()).slice(-2),
    s: ('0' + today.getSeconds()).slice(-2),
    a: today.getDay()
  }
  const yesterday = new Date(today.setDate(todayObj.d - 1))
  const yesterdayObj = {
    y: yesterday.getFullYear(),
    m: ('0' + (yesterday.getMonth() + 1)).slice(-2),
    d: ('0' + yesterday.getDate()).slice(-2),
    h: ('0' + yesterday.getHours()).slice(-2),
    i: ('0' + yesterday.getMinutes()).slice(-2),
    s: ('0' + yesterday.getSeconds()).slice(-2),
    a: yesterday.getDay()
  }
  let date = moment(time)
  const dateObj = {
    y: date.year(),
    m: ('0' + (date.month() + 1)).slice(-2),
    d: ('0' + date.date()).slice(-2),
    h: ('0' + date.hours()).slice(-2),
    i: ('0' + date.minutes()).slice(-2),
    s: ('0' + date.seconds()).slice(-2),
    a: date.day()
  }
  // 今天
  if (todayObj.y === dateObj.y && todayObj.m === dateObj.m && todayObj.d === dateObj.d) {
    return `今天 ${dateObj.h}:${dateObj.i}`
  }
  // 昨天
  if (yesterdayObj.y === dateObj.y && yesterdayObj.m === dateObj.m && yesterdayObj.d === dateObj.d) {
    return `昨天 ${dateObj.h}:${dateObj.i}`
  }
  // 今年
  if (todayObj.y === dateObj.y) {
    return `${dateObj.m}-${dateObj.d} ${dateObj.h}:${dateObj.i}`
  }
  // 非几年 非昨日
  return `${dateObj.y}-${dateObj.m}-${dateObj.d} ${dateObj.h}:${dateObj.i}`
}
